package com.jifeng.sterilizer.utils;

import cn.hutool.core.date.DateUtil;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class GetValueUtils {
    public static String getPhoneStr(String jsonString, String key) {
        String regex = "";
        String val = "";
        switch (key) {
            case "Message":
                regex = "\"Message\":\"(.*?)\\\"";
                break;
            case "RequestId":
                regex = "\"RequestId\":\"(.*?)\\\"";
                break;
            case "Code":
                regex = "\"Code\":\"(.*?)\\\"";
                break;
        }
        //使用非贪婪模式
        Matcher matcher = Pattern.compile(regex).matcher(jsonString);
        while (matcher.find()) {
            val = (matcher.group(1));
        }
        return val;
    }

    /**
     * 大陆号码或香港号码均可
     */
    public static boolean isPhoneLegal(String str) throws PatternSyntaxException {
        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
    }

    /**
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
     * 此方法中前三位格式有：
     * 13+任意数
     * 15+除4的任意数
     * 18+除1和4的任意数
     * 17+除9的任意数
     * 147
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 香港手机号码8位数，5|6|8|9开头+7位任意数
     */
    public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^(5|6|8|9)\\d{7}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    public static int getTimeDifference(Timestamp formatTime1, Timestamp formatTime2) {
        SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
        long t1 = 0L;
        long t2 = 0L;
        try {
            t1 = timeformat.parse(getTimeStampNumberFormat(formatTime1)).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            t2 = timeformat.parse(getTimeStampNumberFormat(formatTime2)).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理
        int hours = (int) ((t1 - t2) / 3600000);
        int minutes = (int) (((t1 - t2) / 1000 - hours * 3600) / 60);
        int second = (int) ((t1 - t2) / 1000 - hours * 3600 - minutes * 60);
        return minutes;
//        return "" + hours + "小时" + minutes + "分" + second + "秒";
    }

    /**
     * 格式化时间
     * Locale是设置语言敏感操作
     *
     * @param formatTime
     * @return
     */
    public static String getTimeStampNumberFormat(Timestamp formatTime) {
        SimpleDateFormat m_format = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss", new Locale("zh", "cn"));
        return m_format.format(formatTime);
    }
}
